using System.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Dm.Migrations.Internal;

public class DmHistoryRepository : HistoryRepository
{
	protected override string ExistsSql
	{
		get
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("SELECT ID FROM SYS.SYSOBJECTS WHERE ");
			if (this.TableSchema != null)
			{
				stringBuilder.Append(" SCHID = (SELECT ID FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCH' AND NAME = ").Append(RelationalTypeMappingSourceExtensions.GetMapping(this.Dependencies.TypeMappingSource, typeof(string)).GenerateSqlLiteral(this.TableSchema)).Append(") AND ");
			}
			else
			{
				stringBuilder.Append(" SCHID = (SELECT ID FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCH' AND NAME = ").Append(" (SELECT SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA'))").Append(") AND ");
			}
			stringBuilder.Append("NAME = ").Append(RelationalTypeMappingSourceExtensions.GetMapping(this.Dependencies.TypeMappingSource, typeof(string)).GenerateSqlLiteral(this.TableName)).Append(";");
			return stringBuilder.ToString();
		}
	}

	public DmHistoryRepository([NotNull] HistoryRepositoryDependencies dependencies)
		: base(dependencies)
	{
	}

	protected override bool InterpretExistsResult(object value)
	{
		return value != null;
	}

	public override string GetCreateIfNotExistsScript()
	{
		IndentedStringBuilder indentedStringBuilder = new IndentedStringBuilder();
		indentedStringBuilder.AppendLine("DECLARE CNT INT; BEGIN ").Append(" SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END INTO CNT FROM SYSOBJECTS WHERE NAME = ").Append(RelationalTypeMappingSourceExtensions.GetMapping(this.Dependencies.TypeMappingSource, typeof(string)).GenerateSqlLiteral(this.TableName))
			.Append(" ");
		if (this.TableSchema != null)
		{
			indentedStringBuilder.Append(" AND ").Append(" SCHID = (SELECT ID FROM SYS.SYSOBJECTS WHERE TYPE$ = 'SCH' AND NAME = ").Append(RelationalTypeMappingSourceExtensions.GetMapping(this.Dependencies.TypeMappingSource, typeof(string)).GenerateSqlLiteral(this.TableSchema))
				.Append(") ");
		}
		indentedStringBuilder.Append(";");
		using (indentedStringBuilder.Indent())
		{
			indentedStringBuilder.Append("IF CNT == 0 THEN ").AppendLines(this.GetCreateScript(), false).Append("END IF; ");
		}
		indentedStringBuilder.AppendLine("END; ");
		return indentedStringBuilder.ToString();
	}

	public override string GetBeginIfNotExistsScript(string migrationId)
	{
		Check.NotEmpty(migrationId, "migrationId");
		return new StringBuilder().AppendLine("DECLARE CNT INT; BEGIN ").Append(" SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END INTO CNT FROM ").Append(this.SqlGenerationHelper.DelimitIdentifier(this.TableName, this.TableSchema))
			.Append(" WHERE ")
			.Append(this.SqlGenerationHelper.DelimitIdentifier(this.MigrationIdColumnName))
			.Append(" = ")
			.Append(RelationalTypeMappingSourceExtensions.GetMapping(this.Dependencies.TypeMappingSource, typeof(string)).GenerateSqlLiteral(migrationId))
			.AppendLine(";")
			.AppendLine("IF CNT == 0 THEN ")
			.ToString();
	}

	public override string GetBeginIfExistsScript(string migrationId)
	{
		Check.NotEmpty(migrationId, "migrationId");
		return new StringBuilder().AppendLine("DECLARE ").AppendLine("   CNT INT;").AppendLine("BEGIN ")
			.Append("SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END INTO CNT FROM ")
			.Append(this.SqlGenerationHelper.DelimitIdentifier(this.TableName, this.TableSchema))
			.Append(" WHERE ")
			.Append(this.SqlGenerationHelper.DelimitIdentifier(this.MigrationIdColumnName))
			.Append(" = ")
			.Append(RelationalTypeMappingSourceExtensions.GetMapping(this.Dependencies.TypeMappingSource, typeof(string)).GenerateSqlLiteral(migrationId))
			.AppendLine(";")
			.AppendLine("IF CNT == 1 THEN ")
			.ToString();
	}

	public override string GetEndIfScript()
	{
		return new StringBuilder().Append("END").AppendLine(this.SqlGenerationHelper.StatementTerminator).ToString();
	}
}
